// components/orderSheet/unpaidOrderItem.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //订单编号
    orderNo: {
      type: String,
      value: '20210715205309'
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: '/images',
    //剩余时间
    leftTime: '已退款',
    //结束时间
    endTime: '2022-11-17',
    orderItems: [{
      itemId: '1',
      itemNum: 1,
      itemImgSrc: '/cart/item.jpg',
      itemName: 'MAGIC DRAGON魔杰龙轻柔倍适学步裤',
      itemDescrition: '尺寸：L',
      itemPrice: '138.00'
    }, {
      itemId: '2',
      itemNum: 1,
      itemImgSrc: '/cart/item.jpg',
      itemName: 'MAGIC DRAGON魔杰龙轻柔倍适学步裤',
      itemDescrition: '尺寸：L',
      itemPrice: '138.00'
    }],
    //价钱合计
    totalPrice: 0,
    //订单详情
    detail: null,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 整数小于10开头加0
     * @param {Number} d 数值
     */
    addZeroHead(d) {
      if (d < 10) return '0' + d;
      return d;
    },
    /**
     * 计算总价
     */
    calTotalPrice() {
      let num = 0.0;
      for (let item of this.data.orderItems) {
        num += Number(item.itemPrice);
      }
      // console.log("num", num)
      this.setData({
        totalPrice: num.toFixed(2)
      })
    },

    /**
     * 立即支付
     * @param {*} e 
     */
    buyAgain(e) {
      this.triggerEvent("buyAgain", this.data.orderNo)
    },
    handleDetail(data) {
      const that=this;
      console.log('datasss',data);
      //保存订单详情
      that.setData({
        detail: data
      })

      //订单项
      let orderItems = [];
      for (const item of data.goods_info) {
        orderItems.push({
          itemId: item.goods_id,
          itemNum: item.buy_number,
          itemImgSrc: item.image,
          itemName: item.goods_name,
          itemDescrition: item.stock_info.spec_value_name,
          itemPrice: item.price,
        })
      }

      //状态


      that.setData({
        endTime: data.complate_pay_time,
        orderItems: orderItems,
      })

      //设置一个定时器，计算付款剩余时间
      // that.calLeftTime();

      //计算总价
      //that.calTotalPrice()

      // console.log("afterServiceOrderItem,orderDetail,that.data", that.data)
    }
  },
  lifetimes: {
    attached() {
      const that = this;

      // console.log("afterServiceOrderItem,attached,orderNo", that.data.orderNo)

      //图片路径
      const util = require("../../utils/util")
      this.setData({
        imgPath: util.getImgPath()
      })

      let orderDetail = wx.getStorageSync('orderDetail');
      // console.log("afterServiceOrderItem,orderDetail", orderDetail)
    //  if (!orderDetail) {

        //取订单详情
        const api = require("../../api/api")
        api.user.orderDetail({
          order_sn: this.data.orderNo,
          success(res) {
             //console.log("afterServiceOrderItem,orderDetail,res", res)

            let data = res.data.data;
           
            that.handleDetail(data)
            that.setData({
               totalPrice: data.price
            });
          }
        })
   //   } 
      // else {
      //   let data = orderDetail
        
      //   that.handleDetail(data)
      // }

      //计算总价
      // this.calTotalPrice()
    }
  }
})